package serialization;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Vector;

/**
 *
 * @author sdecrem
 */

public class SaveVector {
  public static void main(String args[]) throws Exception {
    XMLEncoder encoder =
           new XMLEncoder(
              new BufferedOutputStream(
                new FileOutputStream("/tmp/serial.xml")));
    String data[] = { "Java", "Source", "and", "Support", "."};
    Vector v = new Vector(Arrays.asList(data));
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    ObjectOutputStream o = new ObjectOutputStream(b);
    //
    o.writeObject(v);
    o.close();

    // XML
    encoder.writeObject(v);
    encoder.close() ;

    ByteArrayInputStream bb = new ByteArrayInputStream(b.toByteArray());
    ObjectInputStream oo = new ObjectInputStream(bb);
    //Vector v2 = (Vector) oo.readObject();
    XMLDecoder decoder =
            new XMLDecoder(new BufferedInputStream(
                new FileInputStream("/tmp/serial.xml")));
    Vector v2 = (Vector)decoder.readObject();
    decoder.close();

    Enumeration e = v2.elements();
    while (e.hasMoreElements()) {
      System.out.println(e.nextElement());
    }
  }
}
